package com.flycms.web.front.rest;

import com.flycms.core.base.BaseController;
import com.flycms.core.entity.DataVo;
import com.flycms.core.entity.PageVo;
import com.flycms.core.entity.RestDataVo;
import com.flycms.module.message.model.Message;
import com.flycms.module.message.service.MessageService;
import com.flycms.module.user.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Open source house, All rights reserved
 * 版权：宏石<br/>
 * 开发公司：宏石<br/>
 *
 * @author sun-kaifei
 * @version 1.0 <br/>
 * @email xxx@qq.com
 * @Date: 12:12 2020/11/14
 */
@Slf4j
@Controller
@RequestMapping(value = "rest")
public class MessageController extends BaseController {
    @Autowired
    private MessageService messageService;

    //我的站内短信
    @GetMapping(value = "/u/message/systemSend")
    @ResponseBody
    public RestDataVo userMessage() {
        messageService.addSystemMessage(427530623088078848L,423621789219041280L,"有人给你点赞了","用户huangztd2给你的文章点赞");
        return RestDataVo.OK("发送成功");
    }

    //已读站内短信
    @GetMapping(value = "/u/message/hasView/{id}")
    @ResponseBody
    public RestDataVo messageHasView(@PathVariable String id) {
        messageService.messageHasView(Long.parseLong(id),1);
        return RestDataVo.OK("已读成功");
    }

    //删除站内短信
    @GetMapping(value = "/u/message/del/{id}")
    @ResponseBody
    public RestDataVo messageDel(@PathVariable String id) {
        messageService.deleteMessageById(Long.parseLong(id));
        return RestDataVo.OK("删除成功");
    }

    //查询我的消息
    @GetMapping(value = {"/u/messages/{pageIndex}/{pageSize}/{hasView}","/u/messages/{pageIndex}/{pageSize}"})
    @ResponseBody
    public RestDataVo messageList(@PathVariable(value = "pageIndex", required = false) String pageIndex,
                                  @PathVariable(value = "pageSize", required = false) String pageSize,
                                  @PathVariable(value = "hasView", required = false) Integer hasView) throws Exception {
        User user = getUser();
        if (user == null) {
            return RestDataVo.ERROR("登陆过时，请重新登录后查看消息");
        }
        long toId = user.getUserId();
        PageVo<Message> messageListPage = messageService.getMessageListPage(null, toId, null, null, null,
                hasView, null, null, null, null,
                Integer.parseInt(pageIndex), Integer.parseInt(pageSize));

        return RestDataVo.PAGE(messageListPage.getList(),Integer.parseInt(pageIndex),Integer.parseInt(pageSize),messageListPage.getCount());
    }
}
